﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Model;
using WinnerCMS.Common;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.ControlPanel
{
    [Access("264001")]
    public partial class WordBlackList : WebFactory.PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblPath.Text = "控制面板 →  <a href=\"WordBlackList.aspx\">关键字过滤</a>";


                BindList(1);
            }
        }

        private void BindList(int PageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var pp = new PagingParam();
            pp.PageNo = PageNo;
            pp.PageSize = this.PageNavigation1.PageSize;

            this.GV_List.DataSource = new WordsManage().BindList(0, pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;
        }

        protected void btnAddBL_Click(object sender, EventArgs e)
        {
            Add(0, this.txtFind.Text.Trim(), this.txtReplacement.Text.Trim());

            BindList(1);
        }

        #region 添加或修改

        private void Add(int ID, string Find, string Replacement)
        {
            var model = new Words();
            model.Id = ID;
            model.Find = Find;
            model.Replacement = Replacement;
            model.Ban = 0;
            model.AdminName = UserManage.Current.UserName;

            string strErr = null;

            new WordsManage().Add(model, ref strErr);

            if (string.IsNullOrEmpty(strErr) == false)
            {
                MsgBox.Message(strErr, this.UpdatePanel1);
            }
        }

        #endregion

        protected void GV_List_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var id = (int) this.GV_List.DataKeys[e.RowIndex].Value;
            var Find = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();
            var Replacement = ((TextBox) this.GV_List.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
            Add(id, Find, Replacement);

            this.GV_List.EditIndex = -1;
            BindList(this.PageNavigation1.PageNo);
        }

        protected void GV_List_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var ID = this.GV_List.DataKeys[e.RowIndex].Value.ToString();

            new WordsManage().Delete(ID, this.UpdatePanel1);

            MsgBox.Message("删除成功!", this.UpdatePanel1);

            BindList(this.PageNavigation1.PageNo);
        }

        protected void GV_List_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GV_List.EditIndex = -1;
            BindList(this.PageNavigation1.PageNo);
        }

        protected void GV_List_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GV_List.EditIndex = e.NewEditIndex;
            BindList(this.PageNavigation1.PageNo);
        }

        protected void PageNavigation1_PageChanging(object src, WebControls.PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }
    }
}